<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Validate;
class ArticleCategory extends Base
{
    public function index()
    {
        $key = $this->request->param('key');
        $where = array();
        $where['is_del'] = 0;
        if ($key){
             $where['category_name'] = array('LIKE', '%'.$key.'%');
        }
        $list=model('ArticleCategory')->where($where)->order('category_id desc')->paginate(8);
        // 获取分页显示
        $page = $list->render();
        foreach ($list as $key => $value) {
            $list[$key]['dt'] = date("Y-m-d H:i",$value['ctime']);
            $parent_name=model('ArticleCategory')->where('category_id='.$value['parent_id'])->field('category_name')->find();
            if ($parent_name) {
                $list[$key]['parent_name'] = $parent_name['category_name'];
            }else{
                $list[$key]['parent_name'] = '一级分类';
            }
            
        }
        $this->assign('list', $list);
        $this->assign('page', $page);
    	return $this->fetch();
    }
    // 加载添加/编辑页
    public function add(){
        $category_id = $this->request->param('category_id');
        $dept = model('ArticleCategory')->where('category_id',$category_id)->find();
        $this->assign('data', $dept);
        $list=model('ArticleCategory')->where('parent_id',0)->select();
        $this->assign('list', $list);
        return $this->fetch();	
    }
    //获取分类数据保存
    public function getdata(){
        $rule = [
        'category_name'  => 'require|max:255',
        ];

        $msg = [
            'category_name.require' => '分类名不能为空',
        ];

        $data['category_name'] = $this->request->param('category_name');
        $data['parent_id'] = $this->request->param('parent_id');
        $data['seo_keywords'] = $this->request->param('seo_keywords');
        $data['seo_description'] = $this->request->param('seo_description');
        $data['seo_title'] = $this->request->param('seo_title');
        $data['status'] = $this->request->param('status');
        $data['category_sort'] = $this->request->param('category_sort');
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)){
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }
        $category_id = $this->request->param('category_id');
        if ($category_id){
            $dept_status = model('ArticleCategory')->where('category_id',$category_id)->update($data);
        }else{
            $data['ctime'] = time();
            $dept_status = model('ArticleCategory')->insertGetId($data);
        }
        if ($dept_status){
            echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
        }
    }

    //删除分类
    public function delDept()
    {
        $data['category_id'] = $this->request->param('category_id');
        $where['parent_id'] = $this->request->param('category_id');
        $data['is_del'] = 1;
        $save['is_del'] = 1;
        $dept_status = model('ArticleCategory')->update($data);
        $dept_res = model('ArticleCategory')->where($where)->update($save);
        if ($dept_status){
            echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
        }
    }
}
